/**
 * Created by zhangyi on 2017/11/10.
 */
mainApp.factory("sumweekFactory",function() {
    var service = {};

    //修改时获取数据
    var object;
    service.setSumweek=function(data){
        object=data;

    }
    service.getSumweek=function(){
        return object;
    }

    service.getListData=function(currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 8
        }
        var list = "";
        RequestUtils.postJson('../week1/get',obj).done(function (resp) {
            if(resp.resultCode==0){
                console.log(resp.data);
                list=resp.data;
                CommonUtils.buildFailHint("列表获取","成功");
            }else{
                CommonUtils.buildFailHint('列表获取', '失败');
            }
        });
        return list;
    };

    //新增周清记录service
    service.addSumweekData=function(sumweekObj){
        if(sumweekObj.add_time!=null&&sumweekObj.add_time!=""
            && sumweekObj.create_start_time!=null&&sumweekObj.create_start_time!=""
            && sumweekObj.plan_finish_time!=null&&sumweekObj.plan_finish_time!=""
            && sumweekObj.week_update_time!=null&&sumweekObj.week_update_time!="") {

            RequestUtils.postForm('../week1/add', sumweekObj).done(function (resp) {
                if (resp.resultCode == 0) {
                    CommonUtils.buildSuccessHint('add', "成功");
                } else {
                    CommonUtils.buildFailHint('add', '失败');
                }
            });
        }
    };

    //据id查询service
    service.getSumweekById=function(id){
        var list="";
        RequestUtils.getJson('../week1/get/'+id).done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                list=resp.data;
                CommonUtils.buildFailHint("数据获取","成功");
            }else{
                CommonUtils.buildFailHint('数据获取', '失败');
            }
        });
        return list;
    };

    //修改周清记录
    service.Update=function(sumweekObj){
        if(sumweekObj.add_time!=null&&sumweekObj.add_time!=""
            && sumweekObj.create_start_time!=null&&sumweekObj.create_start_time!=""
            && sumweekObj.plan_finish_time!=null&&sumweekObj.plan_finish_time!=""
            && sumweekObj.week_update_time!=null&&sumweekObj.week_update_time!="") {

            RequestUtils.postForm('../week1/update', sumweekObj).done(function (resp) {
                console.log(resp);
                if (resp.resultCode == 0) {
                    CommonUtils.buildSuccessHint('update', "成功");
                } else {
                    CommonUtils.buildFailHint('update', '失败');
                }
            });
        }
    };

    //按工号和时间区间查询
    service.selectSumweekByJobnumDate=function(sumweekWhereObj,currentindex){
        var obj={
            "pageIndex": currentindex,
            "pageSize": 7
        }
        if(sumweekWhereObj.jobnum=="" || sumweekWhereObj.jobnum==null){
            sumweekWhereObj.jobnum="null";
        }
        if(sumweekWhereObj.startDate=="" || sumweekWhereObj.startDate==null){
            sumweekWhereObj.startDate="null";
        }
        if(sumweekWhereObj.endDate=="" || sumweekWhereObj.endDate==null){
            sumweekWhereObj.endDate="null";
        }

        var list="";
        RequestUtils.postJson('../week1/getByEidTime/'+sumweekWhereObj.jobnum+"/"+sumweekWhereObj.startDate+"/"+sumweekWhereObj.endDate,obj)
            .done(function (resp) {
                if(resp.resultCode==0){
                    list=resp.data;
                    CommonUtils.buildFailHint("列表获取","成功");
                }else{
                    CommonUtils.buildFailHint('列表获取', '失败');
                }
            });
        sumweekWhereObj.jobnum="";
        sumweekWhereObj.startDate="";
        sumweekWhereObj.endDate="";
        return list;
    };

    //删除
    service.deleteSumweek=function(id){
        RequestUtils.getJson('../week1/delete/'+id).done(function (resp) {
            console.log(resp);
            if(resp.resultCode==0){
                CommonUtils.buildSuccessHint('delete',"成功");
                location.reload(true);
            }else{
                CommonUtils.buildFailHint('delete', '失败');
            }
        });
    };

    return service;
});

mainApp.controller("weekListController",function($scope,$http,$state,sumweekFactory) {
    $scope.sumweekList=sumweekFactory.getListData(1);
    $scope.list=$scope.sumweekList.items;

    //跳转修改页面，据id查询
    $scope.selectSumweek=function(id){
        //添加标签
        var tab={url:"sumweek-update",text:"周清详情",id:"sumweek-update-tab",closeable:1};
        $scope.$emit('addTab', tab);

        var sumweekData=sumweekFactory.getSumweekById(id);
        sumweekFactory.setSumweek(sumweekData);
        $state.go('sumweek-update');
    };

    //据工号和时间区间查询
    $scope.sumweekWhere;
    $scope.selectWhere=function(sumweekWhereObj){
        $scope.listShow=false;
        $scope.listByWhereShow=true;
        $scope.sumweekList=sumweekFactory.selectSumweekByJobnumDate(sumweekWhereObj,1);
        $scope.list=$scope.sumweekList.items;
    };

    //删除
    $scope.delete=function(id){
        console.log(id);
        sumweekFactory.deleteSumweek(id);
    };

    $scope.addTab=function(){
        var tab={url:"sumweek-add",text:"周清新增",id:"sumweek-add-tab",closeable:1};
        $scope.$emit('addTab', tab);
    };

    //空值翻页的view
    $scope.listShow=true;
    $scope.listByWhereShow=false;

    //翻页
    $scope.NextListData=function(pageIndex){
        if($scope.listByWhereShow){
            $scope.sumweekList=sumweekFactory.selectDayclearBynumAndDate($scope.sumweekWhere,pageIndex);
        }else{
            $scope.sumweekList=sumweekFactory.getListData(pageIndex);
        }
        $scope.list=$scope.sumweekList.items;
    }

    CommonUtils.setDatetimepicker("startDatetimepicker");
    CommonUtils.setDatetimepicker("endDatetimepicker");
});

mainApp.controller("weekAddController",function($scope,$http,sumweekFactory){
    //新增
    $scope.addSumweek=function(sumweekObj){
        var sumweek={
            "add_time":sumweekObj.add_time,
            "key_tasks":sumweekObj.key_tasks,
            "create_start_time":sumweekObj.create_start_time,
            "plan_finish_time":sumweekObj.plan_finish_time,
            "actual_state":sumweekObj.actual_state,
            "actual_finish_time":sumweekObj.actual_finish_time,
            "week_update_time":sumweekObj.week_update_time,
            "employee.id":1//固定责任人
        };
        console.log(sumweek);
        sumweekFactory.addSumweekData(sumweek);
    };

    $scope.LoginEmp = $cookieStore.get('LoginEmp');

    CommonUtils.setDatetimepicker("oneDatetimepicker");
    CommonUtils.setDatetimepicker("twoDatetimepicker");
    CommonUtils.setDatetimepicker("threeDatetimepicker");
    CommonUtils.setDatetimepicker("fourDatetimepicker");
});

mainApp.controller("weekUpdateController",function($scope,$http,sumweekFactory) {
    //获取修改的数据
    $scope.updateSumweek=sumweekFactory.getSumweek();

    //修改
    $scope.Update=function(updateSumweek){
        var sumweek={
            "id":updateSumweek.id,
            "add_time":updateSumweek.add_time,
            "key_tasks":updateSumweek.key_tasks,
            "create_start_time":updateSumweek.create_start_time,
            "plan_finish_time":updateSumweek.plan_finish_time,
            "actual_state":updateSumweek.actual_state,
            "actual_finish_time":updateSumweek.actual_finish_time,
            "week_update_time":updateSumweek.week_update_time,
            "employee.id":1//固定责任人
        };
        console.log(updateSumweek)
        sumweekFactory.Update(sumweek);

        /*console.log(updateSumweek);
         sumweekFactory.Update(updateSumweek);*/
    };

    CommonUtils.setDatetimepicker("fiveDatetimepicker");
    CommonUtils.setDatetimepicker("sixDatetimepicker");
    CommonUtils.setDatetimepicker("sevenDatetimepicker");
    CommonUtils.setDatetimepicker("eightDatetimepicker");
});